/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

asc
{
  cbuffer VideoDistortVSPara : register(c0) : slot vs 0
  {
    row_major float4x4 mvp;
  };

  cbuffer VideoDistortPSPara : register(c0) : slot ps 0
  {
    float4 Color0;
    float4 Color1;
    float4 Color2;
    float4 Color3;
  };

  cbuffer BlockTransVSPara : register(c0) : slot vs 0
  {
    row_major float4x4 mvp;
  };

  cbuffer Glitch2DVSPara : register(c0) : slot vs 0
  {
    row_major float4x4 mvp;
  };

}

/****************************************************************************/

material VideoDistortMtrl
{
  header
  {
    sBool UseSMH;
    sBool UseGamma;
  }

  prepare
  {
    VertexShader = VS();
    PixelShader = PS();
  }

  vs asc vs_3_0
  {
    use VideoDistortVSPara;
    void main
    (
      in float3 in_pos : POSITION,
      in float4 in_col : COLOR0,
      in float2 in_uv0 : TEXCOORD0,
      out float4 out_pos : POSITION,
      out float2 out_uv0 : TEXCOORD0,
	  out float4 out_col : COLOR0,
    )
    {
	  out_col = in_col;
      out_uv0 = in_uv0;
      out_pos = mul(float4(in_pos,1),mvp);
    }
  }

  ps asc ps_3_0
  {
    use VideoDistortPSPara;
    sampler2D s0 : register(s0);
    void main
    (
	  in float4 vc : COLOR0,
      in float2 uv0 : TEXCOORD0,
      out float4 result : COLOR0,
      )
    {      

	  const float Kry = 0.2126;
	  const float Kby = 0.0722;
	  const float Kgy = 1-(Kry+Kby);
	  const float us  = 2/(1-Kby);
	  const float vs  = 2/(1-Kry);

	  const float4 Kcy = float4(Kry, Kgy, Kby, 0);
 	  const float4 Kcu = float4(-Kry, -Kgy, 1-Kby, 1)/2;
	  const float4 Kcv = float4(1-Kry, -Kgy, -Kby, 1)/2;
	  const float4 Kcr = float4(1, 0, 2, -1);
	  const float4 Kcg = float4(1, 2*(-Kby/Kgy), 2*(-Kry/Kgy), (Kby+Kry)/Kgy);
	  const float4 Kcb = float4(1, 2, 0, -1);

      float4 tex = tex2D(s0,uv0);
	  tex.w = 1;

	  float4 col_yuv;
	  col_yuv.x = dot(tex, Kcy);
	  col_yuv.y = dot(tex, Kcu) + vc.x - vc.y;
	  col_yuv.z = dot(tex, Kcv) + vc.x - vc.y;
	  col_yuv.w = 1;

      result.x = dot(col_yuv, Kcr);
      result.y = dot(col_yuv, Kcg);
      result.z = dot(col_yuv, Kcb);
	  result.w = 1;

	  //result = col_yuv.xxxw;
    }
  }
};

/****************************************************************************/

material BlockTransMtrl
{
  header
  {    
  }

  prepare
  {
    VertexShader = VS();
    PixelShader = PS();
  }

  vs asc vs_3_0
  {
    use BlockTransVSPara;
    void main
    (
      in float3 in_pos : POSITION,
      in float4 in_col : COLOR0,
      in float2 in_uv0 : TEXCOORD0,
      out float4 out_pos : POSITION,
      out float2 out_uv0 : TEXCOORD0,
	  out float4 out_col : COLOR0,
    )
    {
	  out_col = in_col;
      out_uv0 = in_uv0;
      out_pos = mul(float4(in_pos,1),mvp);
    }
  }

  ps asc ps_3_0
  {
    //use BlockTransPSPara;
    sampler2D s0 : register(s0);
    void main
    (
	  in float4 vc : COLOR0,
      in float2 uv0 : TEXCOORD0,
      out float4 result : COLOR0,
      )
    {      
	  float4 tex = tex2D(s0,uv0);
	  tex.w = 1;	 
	  result = tex;//float4(0,1,0,1);
    }
  }
};

/****************************************************************************/

material Glitch2DMtrl
{
  header
  {    
  }

  prepare
  {
    VertexShader = VS();
    PixelShader = PS();
  }

  vs asc vs_3_0
  {
    use Glitch2DVSPara;
    void main
    (
      in float3 in_pos : POSITION,
      in float4 in_col : COLOR0,
      in float2 in_uv0 : TEXCOORD0,
      in float2 in_uv1 : TEXCOORD1,
      out float4 out_pos : POSITION,
      out float2 out_uv0 : TEXCOORD0,
	  out float2 out_uv1 : TEXCOORD1,
	  out float4 out_col : COLOR0,
    )
    {
	  out_col = in_col;
      out_uv0 = in_uv0;
      out_uv1 = in_uv1;
      out_pos = mul(float4(in_pos,1),mvp);
    }
  }

  ps asc ps_3_0
  {
    //use BlockTransPSPara;
    sampler2D s0 : register(s0);
    void main
    (
	  in float4 vc : COLOR0,
      in float2 uv0 : TEXCOORD0,
      in float2 uv1 : TEXCOORD1,
      out float4 result : COLOR0,
      )
    {      
	  float r = tex2D(s0,uv0+float2(uv1.x,0)).r;
	  float g = tex2D(s0,uv0+float2(0,0)).g;
	  float b = tex2D(s0,uv0+float2(-uv1.x,0)).b;
	  result = vc * float4(r,g,b,1);
	  //result = float4(1,0,1,1);
    }
  }
};
